package bid_content

import (
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"io"
)

// 获取招中标详情

func getBidContentPath(publishTime string, websiteName string, id string) string {
	if publishTime == "" {
		return ""
	}
	if len(publishTime) >= 10 {
		publishTime = publishTime[:10]
	}
	if websiteName == "" {
		websiteName = "other"
	}
	return "pub_bid/" + publishTime + "/" + websiteName + "/" + id + ".txt"
}

func getWinBidContentPath(publishTime string, websiteName string, id string) string {
	if publishTime == "" {
		return ""
	}
	if len(publishTime) >= 10 {
		publishTime = publishTime[:10]
	}
	if websiteName == "" {
		websiteName = "other"
	}
	return "win_bid/" + publishTime + "/" + websiteName + "/" + id + ".txt"
}

func GetBidContent(bidType, publishTime, websiteName, id string) (string, error) {
	var ossFilePath string
	if bidType == "bid" {
		ossFilePath = getBidContentPath(publishTime, websiteName, id)
	} else {
		ossFilePath = getWinBidContentPath(publishTime, websiteName, id)
	}
	endpoint := "https://oss-cn-hangzhou.aliyuncs.com"
	accessKeyId := "LTAI5tAkWjrrenAugNcuuWTA"
	accessKeySecret := "4k7QKFjVqps3etGorMUl8LxczA0K0P"
	client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
	bucketName := "gfyx-saas-bid-content"
	if err != nil {
		return "", err
	}
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		return "", err
	}

	object, err := bucket.GetObject(ossFilePath)
	if err != nil {
		return "", err
	}

	content, err := io.ReadAll(object)
	if err != nil {
		return "", err
	}
	return string(content), nil
}
